package com.heima.article.listen;

import com.heima.article.service.ApArticleConfigService;
import com.heima.article.service.ApArticleService;
import com.heima.common.constants.message.NewsUpOrDownConstants;
import com.heima.common.constants.message.PublishArticleConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Slf4j
@Component
public class UpOrDownListener {
    @Autowired
    ApArticleConfigService articleConfigService;
    /**
     * queues: 监听指定队列,队列不存在会报错，不会自动创建
     * queuesToDeclare: 声明并监听指定队列，队列不存在会自动创建
     * bindings: 声明队列  交换机  并通过路由绑定
     */
    @RabbitListener(queuesToDeclare = {@Queue(name = NewsUpOrDownConstants.NEWS_UP_FOR_ARTICLE_CONFIG_QUEUE)})
    public void NewsForUpArticle(String articleId){
        log.info("接收到自媒体文章上架消息,  文章id: {} ",articleId);
        // 自动审核
        try {
            articleConfigService.NewsForUpArticle(Long.valueOf(articleId));
            log.info("自媒体文章上架处理完毕  文章上架成功");
        } catch (NumberFormatException e) {
            e.printStackTrace();
            log.info("article处理 自媒体文章上架消息 失败, 文章id: {} , 失败原因:{}",articleId,e.getMessage());
        }
    }

    @RabbitListener(queuesToDeclare = {@Queue(name = NewsUpOrDownConstants.NEWS_DOWN_FOR_ARTICLE_CONFIG_QUEUE)})
    public void NewsForDownArticle(String articleId){
        log.info("接收到自媒体文章下架消息, 文章id: {} ",articleId);
        // 自动审核
        try {
            articleConfigService.NewsForDownArticle(Long.valueOf(articleId));
            log.info("自媒体文章下架处理完毕  文章下架成功");
        } catch (NumberFormatException e) {
            e.printStackTrace();
            log.info("article处理 自媒体文章上架消息 失败, 文章id: {} , 失败原因:{}",articleId,e.getMessage());
        }
    }
}
